package display.tween 
{
	import flash.events.Event;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	/**
	 * ...
	 * @author deve.huang
	 * 
	 * 位移Tween效果
	 */
	
	public class TweenPos 
	{
		private var _sp:DisplayObject;
		
		private var _tx:int;
		private var _ty:int;
		
		private var _endValue:Number;
		private var _speed:Number;
		
		private var _funcOnEnd:Function;
		
		public function TweenPos(sp:DisplayObject, tx:int, ty:int, endValue:Number = .2, speed:Number = .3, funcOnEnd:Function = null) 
		{
			_funcOnEnd = funcOnEnd;
			_sp = sp;
			
			_tx = tx;
			_ty = ty;
			
			_endValue = endValue;
			_speed = speed;
			
			_sp.addEventListener(Event.ENTER_FRAME, fxHandler);
		}
		
		private function fxHandler(e:Event):void 
		{
			var dx:Number = _tx - _sp.x;
			var dy:Number = _ty - _sp.y;
			_sp.x += dx * _speed;
			_sp.y += dy * _speed;
			if (Math.abs(dx) < _endValue && Math.abs(dy) < _endValue ) {
				_sp.removeEventListener(Event.ENTER_FRAME, fxHandler);
				_sp.x = _tx;
				_sp.y = _ty;
				delete this;
				if (_funcOnEnd != null) {
					_funcOnEnd();
				}
			}
		}
		
	}
}